ActionUtil={}

ActionUtil.createAction = function(plistname, dur, prefix)
  local sfc=CCSpriteFrameCache:sharedSpriteFrameCache()
  local path=CCFileUtils:sharedFileUtils():fullPathFromRelativePath(plistname)
  --print(path)
  local plistdata = CCDictionary:createWithContentsOfFile(path)
  local framedict = TypeCast(plistdata:objectForKey("frames"), "CCDictionary")
  local keys=framedict:allKeys()
 
  sfc:addSpriteFramesWithFile(plistname)
  local animation=CCAnimation:create()
  --print(keys:count())
  local sortedKeys = {}
  for i=0,keys:count()-1 do
    local name = TypeCast(keys:objectAtIndex(i), "CCString")
    local tmp =name:getCString()
    sortedKeys[i+1] = tmp 
  end
  table.sort(sortedKeys)
  for k,v in ipairs(sortedKeys) do
    local b,e = string.find(v, prefix)
    if b==1 then
      --print("key", v)
      local frame = sfc:spriteFrameByName(v)
      animation:addSpriteFrame(frame)
    end
  end
  --[[ 
  for i=0,keys:count()-1 do
    local name = TypeCast(keys:objectAtIndex(i), "CCString")
    local tmp =name:getCString()
    local b,e = string.find(tmp, prefix)
    if b==1 then
      print("key", name:getCString())
      local frame = sfc:spriteFrameByName(name:getCString())
      animation:addSpriteFrame(frame)
    end
  end]]
  

  animation:setDelayPerUnit(dur)
  animation:setRestoreOriginalFrame(true)
  return CCAnimate:create(animation)
  
end

ActionUtil.createSeqAction = function(actions)
  local seq = CCArray:create()
  for i=1,#actions do
    --print("ActionUtil.createSeqAction ",#actions,i, actions[i])
    seq:addObject(actions[i])
  end
  return CCSequence:create(seq)
end

--获取左下角坐标
ActionUtil.leftBottomPoint = function(layer) 
  local tx, ty = layer:getPosition()
  local size = layer:getContentSize()
  local anch = layer:getAnchorPoint()

  local x = tx - size.width*anch.x
  local y = ty - size.height*anch.y
  print("ActionUtil.leftBottomPoint ", x, y)
  return x,y
end

--获取node矩形区域
ActionUtil.getNodeRect = function(node)
  local tx, ty = node:getPosition()
  local size = node:getContentSize()
  local anch = node:getAnchorPoint()

  local x = tx - size.width*anch.x
  local y = ty - size.height*anch.y
  print("ActionUtil.leftBottomPoint ", x, y)
  return CCRectMake(x, y, size.width, size.height)
end

--将矩形缩小rate，并且保证位置不变
ActionUtil.innerRect = function(rect, rate)
  local width = rect.size.width*rate.x
  local height = rect.size.height*rate.y
  
  local x = rect.origin.x + rect.size.width*(1-rate.x)/2;
  local y = rect.origin.y + rect.size.height*(1-rate.y)/2; 

  return CCRectMake(x, y, width, height)
end
--ActionUtil.getAction("a")


